#!python3
# coding: utf-8

import os, re, sys
import subprocess as sp
import multiprocessing as mp

d='dumpbin.exe'

def get_exp(dll, dllpath):
    path=os.path.join(dllpath, dll)
    res = sp.Popen('"{}" /nologo /exports "{}"'.format(d, path), stdout=sp.PIPE)
    ret=[]
    for line in res.stdout:
        line=line.rstrip().decode('cp936')
        m = re.match(r'\s+\d+\s+[0-9A-F]+\s+[0-9A-F]{8}\s+(\S+)', line)
        if m:
            ret.append(m.group(1))
    return (dll, ret)

def main():
    if len(sys.argv) < 2:
        print(f'usage: {sys.argv[0]} dll_path')
        return

    dllp = sys.argv[1]
    if not os.path.exists(dllp):
        return
    pool = mp.Pool()
    
    dlls = [(x, dllp) for x in os.listdir(dllp) if x.lower().endswith('.dll')]
    rets = pool.starmap(get_exp, dlls)
    pool.close()
    pool.join()

    with open('dll_export.csv', 'w') as f:
        for dll, ret in rets:
            for fn in ret:
                f.write('{},{}\n'.format(dll,fn));

if __name__ == "__main__":
    main()
    exit(0)
